package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;


@Service
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    /**
     * 监听MQ的消息队列，接受消息->实现：定时关闭订单功能
     */
    @RabbitHandler
    public void Listener(Message message, OrderEntity entity, Channel channel) throws IOException {
        System.out.println("收到过期订单->订单号:"+entity.getOrderSn());
        try {
            //关闭订单
            orderService.closeOrder(entity);
            //手动收单（关闭支付宝支付页）
            //手动ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception ex){
            //拒收消息且重新放入消息队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }


}
